11651번 좌표 정렬하기 2
Day 13 13단계 20231107
- 11650번 좌표 정렬하기와 순서만 바꾼 동일한 문제다.
- 문제에선 먼저 y 좌표를 오름차순, y 좌표가 같다면 x 좌표를 오름차순으로 정렬해야 한다.
- Arrays 클래스, 함수형 인터페이스
- https://onejunu.tistory.com/53
- https://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array
- https://zara49.tistory.com/149
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
int[][] coord = new int[n][2];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
coord[i][0] = Integer.parseInt(st.nextToken());
coord[i][1] = Integer.parseInt(st.nextToken());
}
Arrays.sort(coord, (a, b) -> {
if(a[1] != b[1]) { return a[1] - b[1]; }
else if (a[0] != b[0]) { return a[0] - b[0]; }
return a[1] - b[1];
});
for (int[] arr : coord) {
sb.append(arr[0] + " " + arr[1] + "\n");
}
System.out.println(sb.toString());
bw.flush();
bw.close();
br.close();
}
}